package com.information.manage.base.sevice.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.information.manage.base.mapper.GongquMapper;
import com.information.manage.base.mapper.ZhanquMapper;
import com.information.manage.base.model.entity.Gongqu;
import com.information.manage.base.model.entity.Zhanqu;
import com.information.manage.base.sevice.ZhanquService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;

@Service
public class ZhanquServiceImpl extends ServiceImpl<ZhanquMapper, Zhanqu> implements ZhanquService {

    @Resource
    private ZhanquMapper zhanquMapper;

    @Resource
    private GongquMapper gongquMapper;

    @Override
    public Page<Zhanqu> getUsersByPage(int currentPage, int pageSize,String name) {
        Page<Zhanqu> page = new Page<>(currentPage, pageSize);
        if(name != null && !name.isEmpty()) {
            LambdaQueryWrapper<Zhanqu> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(Zhanqu::getGqname, name);
            return zhanquMapper.selectPage(page, queryWrapper);
        }
        return zhanquMapper.selectPage(page,null);
    }

    @Override
    public Zhanqu addZhanqu(Zhanqu zhanqu) {
        LambdaQueryWrapper<Gongqu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Gongqu::getGongquid,zhanqu.getGqId());
        zhanqu.setGqname(gongquMapper.selectOne(queryWrapper).getGongquname());
        return zhanqu;
    }

    @Override
    public Boolean updateZhanquById(Zhanqu zhanqu) {
        LambdaUpdateWrapper<Zhanqu> updateWrapper = new LambdaUpdateWrapper<>();
        LambdaQueryWrapper<Gongqu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Gongqu::getGongquid,zhanqu.getGqId());
        updateWrapper.eq(Zhanqu::getZqid,zhanqu.getZqid())
                .set(Zhanqu::getGqId,zhanqu.getGqId())
                .set(Zhanqu::getGqname,gongquMapper.selectOne(queryWrapper).getGongquname());
        return super.update(updateWrapper);
    }

}
